package com.gilight.base.admin.groupshopad;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gilight.base.admin.product.AdminProductService;
import com.gilight.base.base.exception.AdminServiceException;
import com.gilight.base.base.exception.ExceptionDefinition;
import com.gilight.base.base.exception.ServiceException;
import com.gilight.base.component.CacheComponent;
import com.gilight.base.component.CacheConst;
import com.gilight.base.dao.ColdProductMapper;
import com.gilight.base.dto.ColdProductParameterDetailDto;
import com.gilight.base.dto.ColdProductParameterDto;
import com.gilight.base.dto.goods.AdminSpuDTO;
import com.gilight.base.dto.goods.SpuDTO;
import com.gilight.base.dto.goods.SpuTreeNodeDTO;
import com.gilight.base.enums.AdvertUnionType;
import com.gilight.base.enums.BizType;
import com.gilight.base.enums.SpuActivityType;
import com.gilight.base.enums.SpuStatusType;
import com.gilight.base.executor.GlobalExecutor;
import com.gilight.base.mapper.*;
import com.gilight.base.model.*;
import com.gilight.base.model.goods.SkuDO;
import com.gilight.base.model.goods.SpuAttributeDO;
import com.gilight.base.model.goods.SpuSpecificationDO;
import com.gilight.base.model.groupshopad.GroupShopAdDO;
import com.gilight.base.model.spu.SpuDO;
import com.gilight.base.search.SearchEngine;
import com.gilight.base.service.category.CategoryBizService;
import com.gilight.base.service.product.ProductBizService;
import com.gilight.base.storage.StorageBizService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by rize on 2019/7/11.
 */
@Service
public class GroupshopAdServiceImpl implements GroupshopAdService {

    @Autowired
    private GroupshopAdMapper groupshopAdMapper;

    @Autowired
    private CacheComponent cacheComponent;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public String create(GroupShopAdDO groupShopAdDO) throws ServiceException {
        // 1.参数校验
        if (groupShopAdDO.getId() != null) {
            throw new AdminServiceException(ExceptionDefinition.GOODS_CREATE_HAS_ID);
        }
        if(groupShopAdDO.getZzjgId() == null){
            groupShopAdDO.setZzjgId(1L);
        }
        // 2.1.插入主表
        Date now = new Date();
        groupShopAdDO.setGmtUpdate(now);
        groupShopAdDO.setGmtCreate(now);
        groupshopAdMapper.insert(groupShopAdDO);
        return "ok";
    }
    /**
     * 抽取删除缓存公共代码
     */
    private void clearCache(Long zzjgId){
        TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
            @Override
            public void afterCommit() {
                cacheComponent.del(CacheConst.CATEGORY_THREE_LEVEL_TREE);
                cacheComponent.del(CacheConst.CATEGORY_THREE_LEVEL_TREE+"_app:"+zzjgId);
            }
        });
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String edit(GroupShopAdDO groupShopAdDO) throws ServiceException {
        // 1. 参数校验
        if (groupShopAdDO.getId() == null) {
            throw new AdminServiceException(ExceptionDefinition.PARAM_CHECK_FAILED);
        }

        // 将旧值查询出来，若价格没有改变，则无需更新价格排序缓存
        GroupShopAdDO groupShopAdFromDB = groupshopAdMapper.selectById(groupShopAdDO.getId());
        // 2.1. 更新主表
        Date now = new Date();
        groupShopAdFromDB.setGmtUpdate(now);
        groupshopAdMapper.updateById(groupShopAdFromDB);
        return "ok";
    }




    @Override
    public Page<GroupShopAdDO> list(
            Integer page
            ,Integer limit
            ,Long categoryId
            ,String title
            ,String barcode
            ,Integer status
            ,Long zzjgId
            ,Long adminId) throws ServiceException {
        QueryWrapper<GroupShopAdDO> wrapper = new QueryWrapper<GroupShopAdDO>();


        // 3.状态搜索
        if (status != null) {
            wrapper.eq("status", status.intValue() <= SpuStatusType.STOCK.getCode() ? SpuStatusType.STOCK.getCode() : SpuStatusType.SELLING.getCode());
        }

        if(zzjgId != null && zzjgId !=0L){
            wrapper.eq("zzjg_id", zzjgId);
        }
        wrapper.orderByAsc("order_c");
        Page<GroupShopAdDO> dtoPage = groupshopAdMapper.selectPage(Page.div(page, limit, GroupShopAdDO.class), wrapper);
        return dtoPage;
    }

    @Override
    public GroupShopAdDO detail(Long groupShopAdId) throws ServiceException {
        GroupShopAdDO groupShopAdDO = groupshopAdMapper.selectById(groupShopAdId);
        // 0. imgList
        return groupShopAdDO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long groupShopAdId) throws ServiceException {
        // 校验是否有关联此商品的广告
        GroupShopAdDO groupShopAdDO = groupshopAdMapper.selectById(groupShopAdId);
        groupShopAdDO.setStatus(-1);
        return "ok";
    }

    @Override
    public Object getGoodDinnerGroup(Long zzjgId) {
        QueryWrapper<GroupShopAdDO> wrapper = new QueryWrapper<GroupShopAdDO>();
        wrapper.eq("status", SpuStatusType.SELLING.getCode());
        wrapper.eq("zzjg_id", zzjgId);
        wrapper.orderByDesc("order_c");
        wrapper.last("limit 1");
        GroupShopAdDO groupShopAdDO = groupshopAdMapper.selectOne(wrapper);
        return groupShopAdDO;
    }
}
